#!/bin/bash
#SBATCH --job-name=earlytsc-configurator
#SBATCH -c 4
#SBATCH --mem=10G
set -e
SEED=$SLURM_ARRAY_TASK_ID
echo "$(date "+%F %T.%3N") on ${HOSTNAME} ARRAY_TASK_ID:${SEED} ARGS: $@"

ARGS=$@
while [[ $# -gt 0 ]]; do
case "$1" in
    --dataset|--data|-d)
        DATASET="$2"
        shift; shift
        ;;
    --method|-m)
        METHOD="$2"
        shift; shift
        ;;
    --algorithm|--algo)
        ALGO="$2"
        shift; shift
        ;;
    *)
        shift
        ;;
esac
done

# make sure all required variables are set
for VAR in "DATASET" "METHOD" "ALGO" "UCR_ROOT"; do
    if [ -z "${!VAR}" ]; then 
        echo "Variable \"${VAR}\" is not set"
        exit 1
    fi
done

INSTANCE_FILE="${TMP}/UCRsplits/${DATASET}/seed-${SEED}/train_list.txt"
CUTOFF="180"
if [[ $ALGO == *'-long' ]]; then 
    CUTOFF="600"
fi

CMD="../MultiETSC/main --batch "${ARGS}" --seed ${SEED} --instance-file ${INSTANCE_FILE} --cutoff $CUTOFF"
echo $CMD
# Run configurator
srun $CMD
echo

# Add validation runs as separate jobs to the queue
CONF_FILE="output/configurator/${DATASET}/${METHOD}-${ALGO}/run_${SEED}/configs${SEED}.txt"
VALID_FILE="output/configurator/${DATASET}/${METHOD}-${ALGO}/run_${SEED}/validation.csv"
>$VALID_FILE

N_FOLDS=5
N_ARRAY=$(( $(wc -l < $CONF_FILE) * $N_FOLDS - 1))
JOB=$( sbatch --parsable -a 0-$N_ARRAY -o /dev/null --nice=300 \
              --partition=$PARTITION --exclude=$EXCLUDE \
              slurm/run-validate-batch $VALID_FILE $N_FOLDS $INSTANCE_FILE $CONF_FILE $SEED $((CUTOFF + 60)) )


# find test jobs that depend on the current job and add validate jobs as dependency
DJOBS=( $(squeue -u 1036 -ho "%E %A" | grep "afterok:$SLURM_ARRAY_JOB_ID" | perl -pe 's/.* ([0-9]+)/\1/g') )
for DJOB in ${DJOBS[*]}; do
    (   # prevent race conditions by locking
        flock -x 200
        scontrol update jobid=$DJOB Dependency="$(squeue -j $DJOB -ho "%E"),afterok:$JOB"
    ) 200>"$TMP/.lock/c-$DJOB"
done
